package cn.gson.springboot.controller.cuscontroller;

import cn.gson.springboot.aop.MyLog;
import cn.gson.springboot.model.pjos.Culinkman;
import cn.gson.springboot.model.service.cusservice.CulinkmanService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/vue")
public class CulinkmanController {
    @Autowired
    CulinkmanService service;

    //    分页查询联系人
    @RequestMapping("/man-select")
    @ResponseBody
    public Map<String,Object> manSelect(Integer page, Integer size, String cupName){
        Page<Object> obj = PageHelper.startPage(page,size);
        service.manSelect(cupName);
        System.out.println("联系人list:"+service.manSelect(cupName).size());

        Map<String,Object> map=new HashMap<>();
        map.put("totals",obj.getTotal());
        map.put("rows",obj.getResult());
        System.out.println("total:"+obj.getTotal());
        return map;
    }

    //    新增联系人
    @MyLog(type = "联系人",value = "新增了联系人")
    @RequestMapping("/add-linkman")
    @ResponseBody
    public void addLinkman(String culinkman){
//        将字符串转换成JSONObject
        JSONObject obj=(JSONObject)JSONObject.parse(culinkman);
        Culinkman culinkmans=obj.getObject("culinkman",Culinkman.class);
        service.addLinkman(culinkmans);
    }

    //    逻辑删除联系人
    @RequestMapping("/del-culinkman")
    @ResponseBody
    public String delCulinkman(String cupId){
        JSONObject obj=(JSONObject)JSONObject.parse(cupId);
        Integer cupIds=obj.getObject("cupId",Integer.class);
        return service.delCulinkman(cupIds);
    }

    //    查询联系电话是否重复
    @RequestMapping("/select-cupphone")
    @ResponseBody
    public List<Culinkman> selectCupPhone(String cupPhone){
        return service.selectCupPhone(cupPhone);
    }
}
